package cn.wp.ewallet.upms.dao.impl;

import cn.wp.common.core.dao.SqlString;
import cn.wp.ewallet.common.extension.authority.DataAuthority;
import cn.wp.ewallet.common.extension.constant.EwalletConstant;
import cn.wp.ewallet.upms.model.entity.StaffInfoDoo;
import cn.wp.ewallet.upms.dao.StaffInfoDao;
import cn.wp.ewallet.upms.model.entity.StaffInfoDo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import cn.wp.common.core.dao.JdbcSupportTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 员工表数据访问层-数据访问层实现
 * Created by ganjie on 2019/12/24
 */
@Repository
public class StaffInfoDaoImpl implements StaffInfoDao {

    @Autowired
    private JdbcSupportTemplate jdbcSupportTemplate;

    @Override
    public List<StaffInfoDo> selectAllStaffInfos() {

        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select *");
        sqlString.add("from staff_info");
        sqlString.add( "where status != ?");
        return jdbcSupportTemplate.selectList(sqlString.build(),new Object[]{EwalletConstant.INVALID_STATUS},StaffInfoDo.class);
    }

    @Override
    public String selectStaffOrgId(String staffId) {

        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select r.ORG_ID from staff_org_rel r where r.STAFF_ID = ?");
        return jdbcSupportTemplate.selectValue(sqlString.build(),new Object[]{staffId},String.class);
    }

    @Override
    public List<StaffInfoDo> selectOrgStaffInfos(String orgId) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select f.STAFF_NAME, f.STAFF_ID from staff_info f, staff_org_rel r where r.STAFF_ID = f.STAFF_ID and r.ORG_ID = ? and f.`STATUS` != ?");
        return jdbcSupportTemplate.selectList(sqlString.build(),new Object[]{orgId, EwalletConstant.INVALID_STATUS},StaffInfoDo.class);
    }

    @Override
    public StaffInfoDo selectStaffInfo(String staffId) {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select s.staff_id, s.staff_name from staff_info s where s.staff_id = ? and s.status != ?");
        return jdbcSupportTemplate.selectObject(sqlString.build(),new Object[]{staffId, EwalletConstant.INVALID_STATUS},StaffInfoDo.class);
    }

    @Override
    public List<StaffInfoDoo> selectStaffInfoByCondition(String staffName, String roleId) {

        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select su.sys_user_id, su.staff_id, rr.sys_role_id, s.staff_name, p.post_name");
        sqlString.add("    , p.post_id, sor.org_id");
        sqlString.add("from system_user_info su, system_user_role_rel rr, staff_info s, staff_post_rel pr, post_info p, staff_org_rel sor");
        sqlString.add("where su.sys_user_id = rr.sys_user_id");
        sqlString.add("    and s.staff_id = su.staff_id");
        sqlString.add("    and s.staff_id = pr.staff_id");
        sqlString.add("    and p.post_id = pr.post_id");
        sqlString.add("    and s.staff_id = sor.staff_id");
        sqlString.add("    and rr.`status` = ?");
        sqlString.add("    and su.`status` = ?");
        List<String> list = new ArrayList<>();
        list.add(EwalletConstant.STATUS);
        list.add(EwalletConstant.STATUS);

        if (StringUtils.isNotEmpty(staffName)) {
            sqlString.add("    and s.staff_name like ?");
            list.add("%" + staffName + "%");
        }

        if (StringUtils.isNotEmpty(roleId)) {
            sqlString.add("    and rr.sys_role_id = ?");
            list.add(roleId);
        }

        DataAuthority dataAuthority = new DataAuthority(sqlString);

        return jdbcSupportTemplate.selectList(dataAuthority.filterSql(), list.toArray(), StaffInfoDoo.class);
    }

    @Override
    public List<StaffInfoDo> selectAllStaffInfosByAuthority() {
        SqlString sqlString = SqlString.newInstance();
        sqlString.add("select a.*, b.ORG_ID");
        sqlString.add("from staff_info a, STAFF_ORG_REL b, ORGANIZATION_INFO c");
        sqlString.add("where a.STAFF_ID = b.STAFF_ID");
        sqlString.add("    and b.ORG_ID = c.ORG_ID");
        sqlString.add("    and a.status != ? ");
        DataAuthority dataAuthority = new DataAuthority(sqlString);
        return jdbcSupportTemplate.selectList(dataAuthority.filterSql(),new Object[]{EwalletConstant.INVALID_STATUS},StaffInfoDo.class);
    }

}